home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / network / ka9q / nhclb120.zoo / rdate.c < prev    next >
C/C++ Source or Header  |  1992-06-18  |  3KB  |  137 lines

  1. /*
  2.  * rdate
  3.  */
  4. #if RDATE_COMPILES_OK
  5. #include <time.h>
  6. #include <sys/time.h>
  7. #include <ctype.h>
  8. #include "global.h"
  9. #include "mbuf.h"
  10. #include "netuser.h"
  11. #include "timer.h"
  12. #include "udp.h"
  13. #include "iface.h"
  14. #include "ip.h"
  15. #include "internet.h"
  16. #include "cmdparse.h"
  17. #endif
  18.  
  19. #define RDATE_TIMEOUT    30        /* Time limit for booting       */
  20. #define RDATE_RETRANS    5        /* The inteval between sendings */
  21. #define IPPORT_TIMESERVER 37
  22.  
  23. static int SilentStartup = 0;
  24.  
  25. int
  26. dordate(argc,argv,p)
  27. int argc;
  28. char *argv[];
  29. char  *p;  /* why is this here ? */
  30. {
  31. #ifdef RDATE_COMPILES_OK
  32.     struct interface *ifp = NULLIF,*tifp;
  33.     struct socket lsock, fsock;
  34.     struct mbuf *bp;
  35.     struct udp_cb *rdate_cb;
  36.     register char *cp;
  37.     time_t        now,        /* The current time (seconds)   */
  38.               starttime,    /* The start time of sending RDATE */
  39.               lastsendtime;    /* The last time of sending RDATE */
  40.     int i;
  41.     unsigned long newtime;
  42.     long offset = 0;
  43.     struct timeval timeval;
  44.     int32 faddr = 0xffffffff;
  45.     int32 taddr;
  46.  
  47.     ifp = ifaces;         /* default to the first interface */
  48.     if(argc > 1) {
  49.         for(i = 1; i != argc; ++i){
  50.             
  51.             if((tifp = if_lookup(argv[i])) != NULLIF) 
  52.                 ifp = tifp;
  53.             else if(strncmp(argv[i], "silent", strlen(argv[i])) == 0)
  54.                 SilentStartup = 1;
  55.             else if(strncmp(argv[i], "noisy", strlen(argv[i])) == 0)
  56.                 SilentStartup = 0;
  57.             else if (*argv[i] == '-' || isdigit(*argv[i])) {
  58.                     if (*argv[i] == '-')
  59.                     offset = - atoi(argv[i] + 1);
  60.                 else
  61.                     offset = atoi(argv[i]);
  62.             } else if (i == (argc-1) && (taddr = resolve(argv[i])))
  63.                 faddr = taddr;
  64.             else {
  65.                 printf("rdate [net_name] [silent] [noisy] [offset in minutes] [host]\n");
  66.                 return 1;
  67.             }
  68.         }
  69.     }
  70.  
  71.     if(ifp == NULLIF)
  72.         return 0;
  73.     lsock.address = ip_addr;
  74.     lsock.port = 2000;  /* ?? a random number */
  75.  
  76.     open_udp(&lsock,NULLVFP);
  77.  
  78.     fsock.address = faddr;
  79.     fsock.port = IPPORT_TIMESERVER;
  80.  
  81.       /* Get starting time */
  82.       time(&starttime);
  83.       lastsendtime = 0;
  84.  
  85.       /* Send the rdate request packet until a response is received or time
  86.        out */
  87.       for(;;){
  88.  
  89.         /* Get the current time */
  90.         time(&now);
  91.  
  92.         /* Stop, if time out */
  93.         if(now - starttime >= RDATE_TIMEOUT){
  94.             printf("rdate: timed out, time not set\n");
  95.             break;
  96.         }
  97.  
  98.         /* Don't flood the network, send in intervals */
  99.         if(now - lastsendtime > RDATE_RETRANS){
  100.             if(!SilentStartup) printf("Requesting...\n");
  101.  
  102.             /* Allocate BOOTP packet and fill it in */
  103.             if((bp = alloc_mbuf(sizeof(newtime))) == NULLBUF)
  104.                 break;
  105.  
  106.             /* Send out one BOOTP Request packet as a broadcast */
  107.             send_udp(&lsock, &fsock,0,0,bp,sizeof(newtime),0,0);
  108.  
  109.             lastsendtime = now;
  110.         }
  111.  
  112.         /* Give other tasks a chance to run. */
  113. /*        pwait(NULL);*/
  114.         keep_things_going();
  115.  
  116.         /* Test for and process any replies */
  117.         if(recv_udp(&lsock, &fsock, &bp) > -1){
  118.             timeval.tv_sec = pull32(&bp) - 2208988800 + 
  119.               (offset * 60);
  120.             timeval.tv_usec = 0;
  121.             if (settimeofday(&timeval,NULL) == 0) {
  122.               if(!SilentStartup) printf("Date set..\n");
  123.             } else
  124.               printf("rdate: failed to set date\n");
  125.                 break;
  126.         } else if(net_error != WOULDBLK){
  127.             printf("rdate: Net_error %d, no values set\n",
  128.                  net_error);
  129.             break;
  130.         }
  131.       }
  132.  
  133.     del_udp(&lsock);
  134.     return 0;
  135. #endif
  136. }
  137.